/**
 * 
 */
package com.dalonedrow.module.hq.ui.widgets;

import com.dalonedrow.engine.sprite.base.SpriteBase;
import com.dalonedrow.engine.sprite.base.SpriteImageObject;
import com.dalonedrow.engine.sprite.base.SpriteImageObjectFactory;
import com.dalonedrow.engine.sprite.impl.BasicSpriteObject;
import com.dalonedrow.engine.systems.JOGLErrorHandler;
import com.dalonedrow.engine.systems.base.Action;
import com.dalonedrow.engine.systems.base.Condition;
import com.dalonedrow.engine.ui.base.GuiComponent;
import com.dalonedrow.engine.ui.base.border.BorderFactory;
import com.dalonedrow.engine.ui.base.panel.CardPanel;
import com.dalonedrow.engine.ui.base.panel.LandscapePagePanel;
import com.dalonedrow.engine.ui.base.panel.Panel;
import com.dalonedrow.engine.ui.base.panel.PortraitPagePanel;
import com.dalonedrow.engine.ui.base.panel.TabbedPanel;
import com.dalonedrow.engine.ui.base.panel.ThreeStateButton;
import com.dalonedrow.engine.ui.impl.Button;
import com.dalonedrow.engine.ui.impl.Label;
import com.dalonedrow.engine.ui.impl.UIFactory;
import com.dalonedrow.globalvariables.constants.hq.GlobalConstants;
import com.dalonedrow.module.hq.rpg.HQCharacter;
import com.dalonedrow.module.hq.rpg.HQInteractiveObject;
import com.dalonedrow.module.hq.systems.HQEquipment;
import com.dalonedrow.module.hq.systems.HQInventory;
import com.dalonedrow.pooled.PooledStringBuilder;
import com.dalonedrow.pooled.StringBuilderPool;
import com.dalonedrow.utils.Watchable;
import com.dalonedrow.utils.Watcher;

/**
 * UI Viewer class for the {@link HQCharacter} class.
 * @author DaLoneDrau
 */
public final class HQHeroViewer extends PortraitPagePanel
		implements Watcher {
	/** the singleton instance of {@link HQHeroViewer}. */
	private static HQHeroViewer	instance;
	/**
	 * Gets the one and only instance of {@link HQHeroViewer}.
	 * @return {@link HQHeroViewer}
	 */
	public static HQHeroViewer getInstance() {
		if (HQHeroViewer.instance == null) {
			HQHeroViewer.instance = new HQHeroViewer();
		}
		return HQHeroViewer.instance;
	}
	private Button				btnMag0;
	private Button				btnMag1;
	private CardPanel			cards;
	private Label				lblHeroName;
	private Label				lblHeroSprite;
	/** the {@link HQHeroViewer} being viewed. */
	private HQInteractiveObject	pc;
	private Panel				pnlNameEquip;
	private Panel				pnlNameSpells;
	private Panel				pnlNameStats;
	/** the tabbed panel displaying the stats, inventory, and spells panels. */
	private TabbedPanel			tabbedPanel;
	/** the stats panel. */
	// private StatsPanel pnlStats;
	/** Creates a new instance of {@link HQHeroViewer}. */
	private HQHeroViewer() {
		super(Panel.PORTRAIT_CENTER);
		try {
			super.setBorder(WidgetFactory.getInstance().getHeroViewerBorder());
			init();
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * Adds a spell to the hero's knowledge.
	 * @param spellId the spell's id
	 */
	public void addSpellToHerosKnowledge(final int spellId) {
		pc.getPCData().addSpellAvailable(spellId);
	}
	private Panel createCardPanel() throws Exception {
		Panel main = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
		cards = new CardPanel();
		main.addChild(getNamePanel());
		main.addChild(UIFactory.getInstance().getSpacer(1, 3));
		// add buttons

		// fix heights
		int maxWidth = 0, maxHeight = 0, statWidth = 0, statHeight = 0;
		int invWidth = 0, invHeight = 0, magWidth = 0, magHeight = 0;
		// get max for stats panel
		maxWidth = statWidth =
				StatsPanel.getInstance().getPreferredSize().width;
		maxHeight = statHeight =
				StatsPanel.getInstance().getPreferredSize().height;
		// get max for inv panel
		invWidth = HQInventory.getInstance().getPreferredSize().width;
		maxWidth = Math.max(maxWidth, invWidth);
		invHeight = HQInventory.getInstance().getPreferredSize().height;
		maxHeight = Math.max(maxHeight, invHeight);
		// get max for spells panel
		magWidth = HQSpellPanel.getInstance().getPreferredSize().width;
		maxWidth = Math.max(maxWidth, magWidth);
		magHeight = HQSpellPanel.getInstance().getPreferredSize().height;
		maxHeight = Math.max(maxHeight, magHeight);
		// get max width for title panel
		Panel pnlStatsTitle = getStatsSubScreenTitle();
		Panel pnlInvTitle = getInventorySubScreenTitle();
		Panel pnlSpellsTitle = getSpellsSubScreenTitle();
		maxWidth = Math.max(maxWidth, pnlStatsTitle.getPreferredSize().width);
		maxWidth = Math.max(maxWidth, pnlInvTitle.getPreferredSize().width);
		maxWidth = Math.max(maxWidth, pnlSpellsTitle.getPreferredSize().width);

		Panel pnlStats = null, pnlInv = null, pnlSpells = null;
		if (statWidth < maxWidth
				|| statHeight < maxHeight) {
			// add padding
			int neededWidth = maxWidth
					- StatsPanel.getInstance().getPreferredSize().width;
			int neededHeight = maxHeight
					- StatsPanel.getInstance().getPreferredSize().height;
			if (neededWidth > neededHeight) {
				pnlStats = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
				pnlStats.addChild(getStatsPanelButtons());
				pnlStats.addChild(UIFactory.getInstance().getSpacer(1, 3));
				Panel inner = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
				inner.setBorder(
						BorderFactory
								.getInstance()
								.getBorderEdgeOf8ImagesBackgroundOf1Color(
										new int[] { GlobalConstants.INT_004,
												GlobalConstants.INT_004,
												GlobalConstants.INT_004,
												GlobalConstants.INT_004 },
										GlobalConstants.HQ_MONO_INVENTORY_BORDER,
										GlobalConstants.COL_DB_DARK4));
				inner.addChild(pnlStatsTitle);
				inner.addChild(UIFactory.getInstance().getSpacer(1, 3));
				inner.addChild(StatsPanel.getInstance());
				int w1 = neededWidth / 2, w2 = neededWidth - w1;
				int h1 = Math.max(1, maxHeight);
				pnlStats.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				pnlStats.addChild(inner);
				pnlStats.addChild(UIFactory.getInstance().getSpacer(w2, h1));
			} else {
				pnlStats = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
				pnlStats.addChild(getStatsPanelButtons());
				pnlStats.addChild(UIFactory.getInstance().getSpacer(1, 3));
				Panel inner = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
				
				inner.setBorder(
						BorderFactory
								.getInstance()
								.getBorderEdgeOf8ImagesBackgroundOf1Color(
										new int[] { GlobalConstants.INT_004,
												GlobalConstants.INT_004,
												GlobalConstants.INT_004,
												GlobalConstants.INT_004 },
										GlobalConstants.HQ_MONO_INVENTORY_BORDER,
										GlobalConstants.COL_DB_DARK4));
										
				inner.addChild(pnlStatsTitle);
				inner.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int h1 = neededHeight / 2, h2 = neededHeight - h1;
				int w1 = Math.max(1, maxWidth);
				inner.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				inner.addChild(StatsPanel.getInstance());
				inner.addChild(UIFactory.getInstance().getSpacer(w1, h2));
				pnlStats.addChild(inner);
			}
		} else {
			pnlStats = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
			pnlStats.addChild(getStatsPanelButtons());
			pnlStats.addChild(UIFactory.getInstance().getSpacer(1, 3));
			Panel inner = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
			/*
			inner.setBorder(BorderFactory.getInstance()
					.getBorderEdgeOf8ImagesBackgroundOf1Color(
							new int[] { GlobalConstants.INT_004,
									GlobalConstants.INT_004,
									GlobalConstants.INT_004,
									GlobalConstants.INT_004 },
							GlobalConstants.HQ_MONO_INVENTORY_BORDER,
							GlobalConstants.COL_DB_DARK4));
							*/
			inner.addChild(pnlStatsTitle);
			inner.addChild(UIFactory.getInstance().getSpacer(1, 3));
			inner.addChild(UIFactory.getInstance().getSpacer(1, 3));
			inner.addChild(StatsPanel.getInstance());
			pnlStats.addChild(inner);
		}
		cards.addChild("stats", pnlStats);

		if (HQInventory.getInstance().getPreferredSize().width < maxWidth
				|| HQInventory.getInstance().getPreferredSize().height
				< maxHeight) {
			// add padding
			int neededWidth = maxWidth
					- HQInventory.getInstance().getPreferredSize().width;
			int neededHeight = maxHeight
					- HQInventory.getInstance().getPreferredSize().height;
			if (neededWidth > neededHeight) {
				pnlInv = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
				pnlInv.addChild(getInvPanelButtons());
				pnlInv.addChild(UIFactory.getInstance().getSpacer(1, 3));
				pnlInv.addChild(pnlInvTitle);
				pnlInv.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int w1 = neededWidth / 2, w2 = neededWidth - w1;
				int h1 = Math.max(1, maxHeight);
				pnlInv.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				pnlInv.addChild(HQInventory.getInstance());
				pnlInv.addChild(UIFactory.getInstance().getSpacer(w2, h1));
			} else {
				pnlInv = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
				pnlInv.addChild(getInvPanelButtons());
				pnlInv.addChild(UIFactory.getInstance().getSpacer(1, 3));
				Panel inner = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
				inner.setBorder(BorderFactory.getInstance()
						.getBorderEdgeOf8ImagesBackgroundOf1Color(
								new int[] { GlobalConstants.INT_004,
										GlobalConstants.INT_004,
										GlobalConstants.INT_004,
										GlobalConstants.INT_004 },
								GlobalConstants.HQ_MONO_INVENTORY_BORDER,
								GlobalConstants.COL_DB_DARK4));
				inner.addChild(pnlInvTitle);
				inner.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int h1 = neededHeight / 2, h2 = neededHeight - h1;
				int w1 = Math.max(1, maxWidth);
				inner.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				inner.addChild(HQInventory.getInstance());
				inner.addChild(UIFactory.getInstance().getSpacer(w1, h2));
				pnlInv.addChild(inner);
			}
		} else {
			pnlInv = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
			pnlInv.addChild(getInvPanelButtons());
			pnlInv.addChild(UIFactory.getInstance().getSpacer(1, 3));
			Panel inner = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
			inner.setBorder(BorderFactory.getInstance()
					.getBorderEdgeOf8ImagesBackgroundOf1Color(
							new int[] { GlobalConstants.INT_004,
									GlobalConstants.INT_004,
									GlobalConstants.INT_004,
									GlobalConstants.INT_004 },
							GlobalConstants.HQ_MONO_INVENTORY_BORDER,
							GlobalConstants.COL_DB_DARK4));
			inner.addChild(pnlInvTitle);
			inner.addChild(UIFactory.getInstance().getSpacer(1, 3));
			inner.addChild(HQInventory.getInstance());
			pnlInv.addChild(inner);
		}
		cards.addChild("inv", pnlInv);

		if (HQSpellPanel.getInstance().getPreferredSize().width < maxWidth
				|| HQSpellPanel.getInstance().getPreferredSize().height
				< maxHeight) {
			// add padding
			int neededWidth = maxWidth
					- HQSpellPanel.getInstance().getPreferredSize().width;
			int neededHeight = maxHeight
					- HQSpellPanel.getInstance().getPreferredSize().height;
			if (neededWidth > neededHeight) {
				pnlSpells = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
				pnlSpells.addChild(getSpellPanelButtons());
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(1, 3));
				pnlSpells.addChild(pnlSpellsTitle);
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int w1 = neededWidth / 2, w2 = neededWidth - w1;
				int h1 = Math.max(1, maxHeight);
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				pnlSpells.addChild(HQSpellPanel.getInstance());
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(w2, h1));
			} else {
				pnlSpells = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
				pnlSpells.addChild(getSpellPanelButtons());
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(1, 3));
				Panel inner = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
				inner.setBorder(BorderFactory.getInstance()
						.getBorderEdgeOf8ImagesBackgroundOf1Color(
								new int[] { GlobalConstants.INT_004,
										GlobalConstants.INT_004,
										GlobalConstants.INT_004,
										GlobalConstants.INT_004 },
								GlobalConstants.HQ_MONO_INVENTORY_BORDER,
								GlobalConstants.COL_DB_DARK4));
				inner.addChild(pnlSpellsTitle);
				inner.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int h1 = neededHeight / 2, h2 = neededHeight - h1;
				int w1 = Math.max(1, maxWidth);
				inner.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				inner.addChild(HQSpellPanel.getInstance());
				inner.addChild(UIFactory.getInstance().getSpacer(w1, h2));
				pnlSpells.addChild(inner);
			}
		} else {
			pnlSpells = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
			pnlSpells.addChild(getSpellPanelButtons());
			pnlSpells.addChild(UIFactory.getInstance().getSpacer(1, 3));
			Panel inner = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
			inner.setBorder(BorderFactory.getInstance()
					.getBorderEdgeOf8ImagesBackgroundOf1Color(
							new int[] { GlobalConstants.INT_004,
									GlobalConstants.INT_004,
									GlobalConstants.INT_004,
									GlobalConstants.INT_004 },
							GlobalConstants.HQ_MONO_INVENTORY_BORDER,
							GlobalConstants.COL_DB_DARK4));
			inner.addChild(pnlSpellsTitle);
			inner.addChild(UIFactory.getInstance().getSpacer(1, 3));
			inner.addChild(HQSpellPanel.getInstance());
			pnlSpells.addChild(inner);
		}
		cards.addChild("spells", pnlSpells);
		main.addChild(cards);
		return main;
	}
	/**
	 * Create the tabbed panel containing the hero's stats, inventory, and
	 * spells.
	 * @return {@link Panel}
	 * @throws Exception if an error occurs
	 */
	private Panel createTabbedPanel() throws Exception {
		pnlNameStats = getNamePanel();
		pnlNameEquip = getNamePanel();
		pnlNameSpells = getNamePanel();
		// fix heights
		int maxWidth = 0, maxHeight = 0;
		// get max for stats panel
		maxWidth = StatsPanel.getInstance().getPreferredSize().width;
		maxHeight = StatsPanel.getInstance().getPreferredSize().height;
		// get max for inv panel
		maxWidth = Math.max(maxWidth,
				HQInventory.getInstance().getPreferredSize().width);
		maxHeight = Math.max(maxHeight,
				HQInventory.getInstance().getPreferredSize().height);
		// get max for spells panel
		maxWidth = Math.max(maxWidth,
				HQSpellPanel.getInstance().getPreferredSize().width);
		maxHeight = Math.max(maxHeight,
				HQSpellPanel.getInstance().getPreferredSize().height);
		// get max width for name panel
		maxWidth = Math.max(maxWidth, pnlNameStats.getPreferredSize().width);
		maxHeight = Math.max(maxHeight, pnlNameStats.getPreferredSize().height);

		Panel pnlStats = null, pnlInv = null, pnlSpells = null;
		if (StatsPanel.getInstance().getPreferredSize().width < maxWidth
				|| StatsPanel.getInstance().getPreferredSize().height
				< maxHeight) {
			// add padding
			int neededWidth = maxWidth
					- StatsPanel.getInstance().getPreferredSize().width;
			int neededHeight = maxHeight
					- StatsPanel.getInstance().getPreferredSize().height;
			if (neededWidth > neededHeight) {
				pnlStats = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
				pnlStats.addChild(pnlNameStats);
				pnlStats.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int w1 = neededWidth / 2, w2 = neededWidth - w1;
				int h1 = Math.max(1, maxHeight);
				pnlStats.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				pnlStats.addChild(StatsPanel.getInstance());
				pnlStats.addChild(UIFactory.getInstance().getSpacer(w2, h1));
			} else {
				pnlStats = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
				pnlStats.addChild(pnlNameStats);
				pnlStats.addChild(UIFactory.getInstance().getSpacer(1, 3));
				pnlStats.addChild(getLabelPanel("Character"));
				pnlStats.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int h1 = neededHeight / 2, h2 = neededHeight - h1;
				int w1 = Math.max(1, maxWidth);
				pnlStats.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				pnlStats.addChild(StatsPanel.getInstance());
				pnlStats.addChild(UIFactory.getInstance().getSpacer(w1, h2));
			}
		} else {
			pnlStats = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
			pnlStats.addChild(pnlNameStats);
			pnlStats.addChild(UIFactory.getInstance().getSpacer(1, 3));
			pnlStats.addChild(getLabelPanel("Character"));
			pnlStats.addChild(UIFactory.getInstance().getSpacer(1, 3));
			pnlStats.addChild(StatsPanel.getInstance());
		}
		if (HQInventory.getInstance().getPreferredSize().width < maxWidth
				|| HQInventory.getInstance().getPreferredSize().height
				< maxHeight) {
			// add padding
			int neededWidth = maxWidth
					- HQInventory.getInstance().getPreferredSize().width;
			int neededHeight = maxHeight
					- HQInventory.getInstance().getPreferredSize().height;
			if (neededWidth > neededHeight) {
				pnlInv = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
				pnlInv.addChild(pnlNameEquip);
				pnlInv.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int w1 = neededWidth / 2, w2 = neededWidth - w1;
				int h1 = Math.max(1, maxHeight);
				pnlInv.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				pnlInv.addChild(HQInventory.getInstance());
				pnlInv.addChild(UIFactory.getInstance().getSpacer(w2, h1));
			} else {
				pnlInv = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
				pnlInv.addChild(pnlNameEquip);
				pnlInv.addChild(UIFactory.getInstance().getSpacer(1, 3));
				pnlInv.addChild(getLabelPanel("INVENTORY"));
				pnlInv.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int h1 = neededHeight / 2, h2 = neededHeight - h1;
				int w1 = Math.max(1, maxWidth);
				pnlInv.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				pnlInv.addChild(HQInventory.getInstance());
				pnlInv.addChild(UIFactory.getInstance().getSpacer(w1, h2));
			}
		} else {
			pnlInv = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
			pnlInv.addChild(pnlNameEquip);
			pnlInv.addChild(UIFactory.getInstance().getSpacer(1, 3));
			pnlInv.addChild(getLabelPanel("INVENTORY"));
			pnlInv.addChild(UIFactory.getInstance().getSpacer(1, 3));
			pnlInv.addChild(HQInventory.getInstance());
		}
		if (HQSpellPanel.getInstance().getPreferredSize().width < maxWidth
				|| HQSpellPanel.getInstance().getPreferredSize().height
				< maxHeight) {
			// add padding
			int neededWidth = maxWidth
					- HQSpellPanel.getInstance().getPreferredSize().width;
			int neededHeight = maxHeight
					- HQSpellPanel.getInstance().getPreferredSize().height;
			if (neededWidth > neededHeight) {
				pnlSpells = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
				pnlSpells.addChild(pnlNameSpells);
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int w1 = neededWidth / 2, w2 = neededWidth - w1;
				int h1 = Math.max(1, maxHeight);
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				pnlSpells.addChild(HQSpellPanel.getInstance());
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(w2, h1));
			} else {
				pnlSpells = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
				pnlSpells.addChild(pnlNameSpells);
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(1, 3));
				pnlSpells.addChild(getLabelPanel("SPELLS"));
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(1, 3));
				int h1 = neededHeight / 2, h2 = neededHeight - h1;
				int w1 = Math.max(1, maxWidth);
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(w1, h1));
				pnlSpells.addChild(HQSpellPanel.getInstance());
				pnlSpells.addChild(UIFactory.getInstance().getSpacer(w1, h2));
			}
		} else {
			pnlSpells = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
			pnlSpells.addChild(pnlNameSpells);
			pnlSpells.addChild(UIFactory.getInstance().getSpacer(1, 3));
			pnlSpells.addChild(getLabelPanel("SPELLS"));
			pnlSpells.addChild(UIFactory.getInstance().getSpacer(1, 3));
			pnlSpells.addChild(HQSpellPanel.getInstance());
		}
		// left side tabs
		tabbedPanel = new TabbedPanel(Panel.PAGELAYOUT_PORTRAIT, true);
		tabbedPanel.addTab(
				getStatisticsTabButton(),
				pnlStats,
				"stats"
				);
		tabbedPanel.addTab(
				getInventoryTabButton(),
				pnlInv,
				"inv"
				);
		tabbedPanel.addTab(
				getSpellsTabButton(),
				pnlSpells,
				"spells"
				);
		tabbedPanel.setBorder(
				GlobalConstants.DQ_CHARVIEWER_BORDER,
				new float[][] {
						GlobalConstants.COL_DQ_DARK_GREY,
						GlobalConstants.COL_DQ_DARK_GREY,
						GlobalConstants.COL_DQ_DARK_GREY
				},
				// tab insets
				new int[] { GlobalConstants.INT_006, GlobalConstants.INT_006,
						GlobalConstants.INT_006, GlobalConstants.INT_006 },
				// content insets
				new int[] { GlobalConstants.INT_009, GlobalConstants.INT_009,
						GlobalConstants.INT_006, GlobalConstants.INT_009 },
				// background color insets
				new int[] { 2, 2, 0, 0 }
				);
		return tabbedPanel;
	}
	private Panel getInventorySubScreenTitle() throws Exception {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		pnl.addChild(UIFactory.getInstance().getStandardImageLabel(
				"icon_inventory"));
		pnl.addChild(UIFactory.getInstance().getSpacer(2, 1));
		pnl.addChild(UIFactory.getInstance().getStandardH2Label("Inventory"));
		return pnl;
	}
	/**
	 * Gets the inventory tab button.
	 * @return {@link ThreeStateButton}
	 * @throws Exception if an error occurs
	 */
	private ThreeStateButton getInventoryTabButton() throws Exception {
		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"hq_ultima_icon_equip_sml");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Label lbl1 = new Label(sb);

		sb = new BasicSpriteObject();
		img = SpriteImageObjectFactory.getInstance().getImageByName(
				"hq_ultima_icon_equip_lrg");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Button btn2 = new Button(sb);
		btn2.setTooltipText("inventory");

		sb = new BasicSpriteObject();
		img = SpriteImageObjectFactory.getInstance().getImageByName(
				"hq_ultima_icon_equip_act");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Label lbl3 = new Label(sb);
		ThreeStateButton btn3S = new ThreeStateButton(lbl1, btn2, lbl3);
		btn3S.setCanStayOn3(new Condition() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public boolean passes() {
				return HQInventory.getInstance().isVisible();
			}
		});
		btn3S.setHorizontalAlignment(GuiComponent.RIGHT);
		return btn3S;
	}
	/**
	 * Gets the action buttons for the spells panel.
	 * @return {@link Panel}
	 * @throws Exception if an error occurs
	 */
	private Panel getInvPanelButtons() throws Exception {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		Button btnStats = UIFactory.getInstance().getStandardImageButton(
				"btn_stats_inactive",
				"btn_stats_inactive_highlighted");
		btnStats.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				cards.displayChild("stats");
			}
		});
		pnl.addChild(btnStats);
		pnl.addChild(UIFactory.getInstance().getSpacer(GlobalConstants.INT_010,
				1));
		btnStats = null;

		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"btn_inv_active");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Button btnInv = new Button(sb);
		pnl.addChild(btnInv);
		pnl.addChild(UIFactory.getInstance().getSpacer(GlobalConstants.INT_010,
				1));
		sb = null;
		img = null;
		btnInv = null;

		btnMag1 =
				UIFactory.getInstance().getStandardImageButton(
						"btn_spells_inactive",
						"btn_spells_inactive_highlighted",
						"btn_spells_disabled");
		btnMag1.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				cards.displayChild("spells");
			}
		});
		btnMag1.setActive(false);
		pnl.addChild(btnMag1);
		return pnl;
	}
	/**
	 * Gets the name panel.
	 * @return {@link Panel}
	 * @throws Exception if an error occurs
	 */
	private Panel getLabelPanel(final String text) throws Exception {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		pnl.setBorder(BorderFactory.getInstance().getBorderEdgeOf8Images(
				new int[] {
						GlobalConstants.INT_003, GlobalConstants.INT_003,
						GlobalConstants.INT_003, GlobalConstants.INT_003
				}, // insets
				GlobalConstants.HQ_MONO_NAMEPANEL_BORDER)
				);
		Label lbl = UIFactory.getInstance().getTitleLabel(text);
		pnl.addChild(lbl);
		return pnl;
	}
	/**
	 * Gets the name panel.
	 * @return {@link Panel}
	 * @throws Exception if an error occurs
	 */
	private Panel getNamePanel() throws Exception {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		pnl.setBorder(BorderFactory.getInstance()
				.getBorderEdgeOf8ImagesBackgroundOf1Color(
						new int[] { GlobalConstants.INT_004,
								GlobalConstants.INT_004,
								GlobalConstants.INT_004,
								GlobalConstants.INT_004
						},
						GlobalConstants.STAT_SUB_SCREEN_BORDER_2,
						GlobalConstants.COL_DB_DARK4, 2, 2));
		// 32px
		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"ultima_barbarian_zull");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		lblHeroSprite = new Label(sb);
		pnl.addChild(lblHeroSprite);
		lblHeroName = UIFactory.getInstance().getStandardH1Label(
				" \n ");
		// set width to max of 9 characters
		lblHeroName.setPreferredWidth(GlobalConstants.INT_009
				* UIFactory.getInstance().getH1FontCharacterMetrics().width);
		pnl.addChild(lblHeroName);
		return pnl;
	}
	/**
	 * Gets the pc being viewed.
	 * @return {@link HQInteractiveObject}
	 */
	protected HQInteractiveObject getPc() {
		return pc;
	}
	/**
	 * Gets the action buttons for the inventory panel.
	 * @return {@link Panel}
	 * @throws Exception if an error occurs
	 */
	private Panel getSpellPanelButtons() throws Exception {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		Button btnStats = UIFactory.getInstance().getStandardImageButton(
				"btn_stats_inactive",
				"btn_stats_inactive_highlighted");
		btnStats.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				cards.displayChild("stats");
			}
		});
		pnl.addChild(btnStats);
		pnl.addChild(UIFactory.getInstance().getSpacer(GlobalConstants.INT_010,
				1));
		btnStats = null;

		Button btnInv =
				UIFactory.getInstance().getStandardImageButton(
						"btn_inv_inactive", "btn_inv_inactive_highlighted");
		btnInv.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				cards.displayChild("inv");
			}
		});
		pnl.addChild(btnInv);
		pnl.addChild(UIFactory.getInstance().getSpacer(GlobalConstants.INT_010,
				1));
		btnInv = null;

		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"btn_spells_active");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Button btnSpells = new Button(sb);
		pnl.addChild(btnSpells);
		sb = null;
		img = null;
		btnSpells = null;
		return pnl;
	}
	private Panel getSpellsSubScreenTitle() throws Exception {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		pnl.addChild(UIFactory.getInstance().getStandardImageLabel(
				"icon_spells"));
		pnl.addChild(UIFactory.getInstance().getSpacer(2, 1));
		pnl.addChild(UIFactory.getInstance().getStandardH2Label("Thaumaturgy"));
		return pnl;
	}
	/**
	 * Gets the spells tab button.
	 * @return {@link ThreeStateButton}
	 * @throws Exception if an error occurs
	 */
	private ThreeStateButton getSpellsTabButton() throws Exception {
		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"hq_ultima_icon_mag_sml");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Label lbl1 = new Label(sb);
		lbl1.setInactiveImage(
				SpriteImageObjectFactory.getInstance().getImageRefId(
						"hq_ultima_icon_mag_inact"));

		sb = new BasicSpriteObject();
		img = SpriteImageObjectFactory.getInstance().getImageByName(
				"hq_ultima_icon_mag_lrg");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Button btn2 = new Button(sb);
		btn2.setTooltipText("spells");

		sb = new BasicSpriteObject();
		img = SpriteImageObjectFactory.getInstance().getImageByName(
				"hq_ultima_icon_mag_act");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Label lbl3 = new Label(sb);
		ThreeStateButton btn3S = new ThreeStateButton(lbl1, btn2, lbl3);
		btn3S.setCanStayOn3(new Condition() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public boolean passes() {
				return HQSpellPanel.getInstance().isVisible();
			}
		});
		btn3S.setHorizontalAlignment(GuiComponent.RIGHT);
		return btn3S;
	}
	/**
	 * Gets the statistics tab button.
	 * @return {@link ThreeStateButton}
	 * @throws Exception if an error occurs
	 */
	private ThreeStateButton getStatisticsTabButton() throws Exception {
		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"hq_ultima_icon_stats_sml");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Label lbl1 = new Label(sb);

		sb = new BasicSpriteObject();
		img = SpriteImageObjectFactory.getInstance().getImageByName(
				"hq_ultima_icon_stats_lrg");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Button btn2 = new Button(sb);
		btn2.setTooltipText("Character panel");

		sb = new BasicSpriteObject();
		img = SpriteImageObjectFactory.getInstance().getImageByName(
				"hq_ultima_icon_stats_act");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Label lbl3 = new Label(sb);
		ThreeStateButton btn3S = new ThreeStateButton(lbl1, btn2, lbl3);
		btn3S.setCanStayOn3(new Condition() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public boolean passes() {
				return StatsPanel.getInstance().isVisible();
			}
		});
		btn3S.setHorizontalAlignment(GuiComponent.RIGHT);
		return btn3S;
	}
	/**
	 * Gets the action buttons for the statistics panel.
	 * @return {@link Panel}
	 * @throws Exception if an error occurs
	 */
	private Panel getStatsPanelButtons() throws Exception {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"btn_stats_active");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Button btnStats = new Button(sb);
		pnl.addChild(btnStats);
		pnl.addChild(UIFactory.getInstance().getSpacer(GlobalConstants.INT_010,
				1));
		sb = null;
		img = null;
		btnStats = null;

		Button btnInv =
				UIFactory.getInstance().getStandardImageButton(
						"btn_inv_inactive", "btn_inv_inactive_highlighted");
		btnInv.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				cards.displayChild("inv");
			}
		});
		pnl.addChild(btnInv);
		pnl.addChild(UIFactory.getInstance().getSpacer(GlobalConstants.INT_010,
				1));
		btnInv = null;

		btnMag0 =
				UIFactory.getInstance().getStandardImageButton(
						"btn_spells_inactive",
						"btn_spells_inactive_highlighted",
						"btn_spells_disabled");
		btnMag0.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				cards.displayChild("spells");
			}
		});
		btnMag0.setActive(false);
		pnl.addChild(btnMag0);
		return pnl;
	}
	private Panel getStatsSubScreenTitle() throws Exception {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		pnl.addChild(UIFactory.getInstance().getStandardImageLabel(
				"icon_anatomy"));
		pnl.addChild(UIFactory.getInstance().getSpacer(2, 1));
		pnl.addChild(UIFactory.getInstance().getStandardH2Label("ANATOMY"));
		return pnl;
	}
	/**
	 * Initializes the viewer.
	 * @throws Exception if an error occurs
	 */
	private void init() throws Exception {
		// 1st row - Sprite, Name
		super.addChild(createCardPanel());
		// super.addChild(getStatsPanel());
		// super.addChild(createNamePanel());
		// 2nd row stats/inv/spells
		// super.addChild(createTabbedPanel());
		// createTabbedPanel();
		// 2nd row - description
		// super.addChild(createDescriptionPanel());
		// tabbedPanel.showTab("inv");
	}
	/**
	 * Sets the character being viewed.
	 * @param newPc the {@link HQCharacter} to set
	 */
	public void setPc(final HQInteractiveObject newPc) {
		try {
			if (pc != null
					&& pc.hasIOFlag(GlobalConstants.IO_01_PC)) {
				pc.getPCData().removeWatcher(this);
			}
			pc = newPc;
			pc.getPCData().addWatcher(this);
			pc.getPCData().clearSpellsAvailable();
			HQInventory.getInstance().setPlayerViewed(pc);
			HQSpellPanel.getInstance().setPlayerViewed(pc);
			if (pc.getPCData().isMagician()) {
				btnMag0.setActive(true);
				btnMag1.setActive(true);
			} else {
				btnMag0.setActive(false);
				btnMag0.getSprite().setImageRefId(
						SpriteImageObjectFactory.getInstance().getImageRefId(
								"btn_spells_disabled"));
				btnMag1.setActive(false);
			}
			cards.displayChild("stats");
			/*
			 * if (pc.getPCData().isMagician()) {
			 * tabbedPanel.getTab(2).setActive(true); } else {
			 * tabbedPanel.getTab(2).setActive(false); }
			 * tabbedPanel.showTab("stats");
			 */
			watchUpdated(null);
			/*
			 * if (pc.getPCData().isMagician()) { // show spell chooser if
			 * (pc.getPCData().getProfession() == GlobalConstants.CLASS_02ELF) {
			 * SpellFamilyChooserDialog.getInstance().setNumAllowed(1); } else {
			 * SpellFamilyChooserDialog.getInstance().setNumAllowed(
			 * GlobalConstants.INT_003); } Point pt =
			 * UtilityMethods.getInstance().centerComponentInFrame(
			 * SpellFamilyChooserDialog.getInstance());
			 * SpellFamilyChooserDialog.getInstance().setPosition( pt.x, pt.y,
			 * 1000); Interface.getInstance().showDialog(
			 * SpellFamilyChooserDialog.getInstance()); }
			 */
			System.out.println(pc.getPCData().getName() + "::wpn::"
					+ HQEquipment.getInstance().getPlayerWeaponType(pc));
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void watchUpdated(final Watchable data) {
		// set character sprite
		/*
		 * Label lblSprite = (Label) pnlNameEquip.getChildren()[0];
		 * lblSprite.getSprite().setImageRefId(pc.getPCData().getSpriteRef());
		 * lblSprite = (Label) pnlNameSpells.getChildren()[0];
		 * lblSprite.getSprite().setImageRefId(pc.getPCData().getSpriteRef());
		 * lblSprite = (Label) pnlNameStats.getChildren()[0];
		 * lblSprite.getSprite().setImageRefId(pc.getPCData().getSpriteRef());
		 * lblSprite = null;
		 */
		lblHeroSprite.getSprite().setImageRefId(pc.getPCData().getSpriteRef());
		// name
		PooledStringBuilder sb =
				StringBuilderPool.getInstance().getStringBuilder();
		sb.append(pc.getPCData().getName());
		sb.append(" the ");
		sb.append(GlobalConstants.CLASS_TITLES[pc.getPCData().getProfession()]);
		lblHeroName.setText(sb.toString());
		/*
		 * Label lblName = (Label) pnlNameEquip.getChildren()[1];
		 * lblName.setText(sb.toString()); lblName = (Label)
		 * pnlNameSpells.getChildren()[1]; lblName.setText(sb.toString());
		 * lblName = (Label) pnlNameStats.getChildren()[1];
		 * lblName.setText(sb.toString()); lblName = null;
		 */
		sb.setLength(0);
		sb.returnToPool();
		StatsPanel.getInstance().update(pc.getPCData());
	}

}
